package cn.tom.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Data
public class User implements UserDetails {
    /**
     * Logger输出
     */
    @TableField(exist = false)
    private Logger log
            = LoggerFactory.getLogger(cn.tom.tool.Log.class);

    private Integer id;                  //用户的ID
    private String account;          //用户的账号
    private String password;         //用户的密码
    private String role;             //用户的角色
    private Integer phone;           //用户的电话
    private String nickname;         //用户的昵称
    private Integer closed = 1;        //是否激活  0关闭/1激活
    private String picture;          //用户头像

    @Override//拿角色(权限)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        String aa[] = this.role.split(",");
        List<SimpleGrantedAuthority> lst = new ArrayList<>();
        for (String s : aa) {
            SimpleGrantedAuthority sga = new SimpleGrantedAuthority(s.trim());
            log.info("权限：" + s);
            lst.add(sga);
        }
        return lst;

    }

    @Override
    public String getUsername() {
        return this.account = account;
    }

    //重写UserDetails的方法
    @Override
    //账号是否过期
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    //是否锁住
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    //密码是否过期
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    //账号是否激活
    public boolean isEnabled() {
        return true;
    }

}
